﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MLY.Bll;

namespace MLY.LTR
{
  public partial class ClientsCard : BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Expires = 0;
      //  Response.Cache.SetNoStore();
      Response.AppendHeader("Pragma", "no-cache");

      if (!Page.IsPostBack)
      {
        ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];
        lblRelationExistsErr.Visible = false;
        lblSuccessfullSaving.Visible = false;


        int contractorId = SessParam.OrganizationId;
        int clientId;

        if ((contractorId > 0) &&
            g.ParseQueryString(Request.QueryString, "ClientId", out clientId))
        {
          if (clientId != -1)
          {
            subContractorRow.Visible = false;
            clsOrganization con = new clsOrganization(clientId);
            clsOrganizationRelation rel = new clsOrganizationRelation(clientId, contractorId);

            this.lblContactPersonName.Text = con.Data.ContactPerson;
            this.lblContactPersonEmail.Text = con.Data.ContactPersonEmail;
            this.lblContactPersonPhone.Text = con.Data.ContactPersonPhone;
            this.txtClientCode.Text = rel.Data.CustomerCode;
            this.txtDefaultCost.Text = g.FormatDecimalAmount(rel.Data.DefaultCost);
            this.txtDefaultPeriodDays.Text = rel.Data.DefaultTimeDurationDays.ToString();
            this.txtDeviationParam.Text = rel.Data.DefaultDeviationParameter.ToString();
          }
          else
          {
            subContractorRow.Visible = true;
          }
        }
      }
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
      int ContractorId = SessParam.OrganizationId;
      int ClientId;
      clsOrganization client, contractor;
      if ((ContractorId > 0) &&
          g.ParseQueryString(Request.QueryString, "ClientId", out ClientId))
      {
        if (ClientId != -1)
        {
          clsOrganizationRelation rel = new clsOrganizationRelation(ClientId, ContractorId);

          rel.Data.CustomerCode = this.txtClientCode.Text;
          rel.Data.DefaultCost = g.ParseDecimal(this.txtDefaultCost.Text);
          rel.Data.DefaultTimeDurationDays = g.ParseDecimal(this.txtDefaultPeriodDays.Text);
          rel.Save();
          client = new clsOrganization(rel.Data.CustomerId.Value);
          contractor = new clsOrganization(rel.Data.ContractorId.Value);
          lblSuccessfullSaving.Visible = true;
        }
        else
        {
          // Do not allow to insert a new record to TableOrganizationRelations for an existing relation
          //
          clsOrganizationRelation rel = new clsOrganizationRelation();

          rel.Data.CustomerId = int.Parse(ddlClientsList.SelectedValue);
          rel.Data.ContractorId = ContractorId;
          rel.Data.CustomerCode = this.txtClientCode.Text;
          rel.Data.DefaultCost = g.ParseDecimal(this.txtDefaultCost.Text);
          rel.Data.DefaultTimeDurationDays = g.ParseDecimal(this.txtDefaultPeriodDays.Text);
          rel.Data.DefaultDeviationParameter = g.ParseInt(this.txtDeviationParam.Text);
          rel.Save();
          client = new clsOrganization(rel.Data.CustomerId.Value);
          contractor = new clsOrganization(rel.Data.ContractorId.Value);
          lblSuccessfullSaving.Visible = true;
          lblRelationExistsErr.Visible = false;
        }
        string subject = "MLY automatic system massage";
        string content = String.Format(@"Hello  <br />This is automatic system massage <br />
          Your organization's connection with {0} was established or updated. <br /> 
          Best Regards, <br />
          MLY team", contractor.Data.OrganizationName);
        Utils.mlySendMail(subject,
          contractor.Data.ContactPersonEmail + "," + client.Data.ContactPersonEmail + "," + Utils.AdminMail, "", "", content, true, MLY.Utils.MailTemplate.AddContractor);
      }
    }

    protected void btnClose_Click(object sender, ImageClickEventArgs e)
    {
      Response.RedirectTo((string)ViewState["Referer"]);
    }

    protected void ddlClientsList_SelectedIndexChanged(object sender, EventArgs e)
    {
      clsOrganization con = new clsOrganization(int.Parse(ddlClientsList.SelectedValue));
      this.lblContactPersonName.Text = con.Data.ContactPerson;
      this.lblContactPersonEmail.Text = con.Data.ContactPersonEmail;
      this.lblContactPersonPhone.Text = con.Data.ContactPersonPhone;

    }

  }
}
